const { TaskAssignment } = require('../../database/index')
const { getExecutedSql } = require('../../logger');
// 导出
module.exports = async (req, res) =>
{
    const operation = req.params.operation;
    const { assignedToUserID, workOrderID, assignmentID, assignmentDate, deadline } = req.body
    try {
        switch (operation) {
            case 'create':
                const newAssignedData = await TaskAssignment.create({
                    WorkOrderID: workOrderID,
                    AssignedToUserID: assignedToUserID,
                    AssignmentDate: new Date(),//分配日期
                    Deadline: deadline//截止日期
                })
                res.status(200).send({
                    data: newAssignedData,
                    meta: {
                        msg: "创建成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'read':
                const assignedData = await TaskAssignment.findAll()
                res.status(200).send({
                    data: assignedData,
                    meta: {
                        msg: "获取成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'update':
                const stateData = {
                    WorkOrderID: workOrderID,
                    AssignedToUserID: assignedToUserID,
                    AssignmentDate: assignmentDate,//分配日期
                    Deadline: deadline//截止日期
                }
                const upAssignedData = await TaskAssignment.update(stateData, { where: { AssignmentID: assignmentID } },)
                res.status(200).send({
                    meta: {
                        msg: "编辑成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'delete':
                console.log(assignmentID, "====>");
                const delData = await TaskAssignment.destroy({ where: { AssignmentID: assignmentID } })
                res.status(200).send({
                    meta: {
                        msg: "删除成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            default:
                res.status(400).send({ message: 'Invalid operation' });
        }
    } catch (error) {
        res.status(500).send({ message: 'Internal server error', error: error.message });
    }
}